package competition.cig.cs478;

import ch.idsia.ai.agents.Agent;
import ch.idsia.mario.environments.Environment;

import competition.cig.cs478.backprop.Network;

public class CS478_TweenAgent implements Agent {
	
	private CS478_BackpropAgent backprop;
	private CS478_AStar_Backprop_Learner astar;
	private Agent activeAgent;
	private int astarPeriod = 20;
	private int backpropPeriod = 4;
	private int toggleCountdown;
	
	public CS478_TweenAgent(Network network) {
		backprop = new CS478_BackpropAgent(network);
		astar = new CS478_AStar_Backprop_Learner(network);
		activeAgent = astar;
		toggleCountdown = astarPeriod;
	}
	
	private void toggleAgents() {
		if (activeAgent instanceof CS478_BackpropAgent) {
			activeAgent = astar;
			toggleCountdown = astarPeriod;
		}
		else {
			activeAgent = backprop;
			toggleCountdown = backpropPeriod;
		}
	}
	
	public void setAstarPeriod(int p) {
		this.astarPeriod = p;
	}
	
	public void setBackpropPeriod(int p) {
		this.backpropPeriod = p;
	}
	
	public void setNetwork(Network network) {
		this.backprop.setNetwork(network);
		this.astar.setNetwork(network);
	}

	@Override
	public void reset() {
		activeAgent = astar;
		astar.reset();
		backprop.reset();
	}

	@Override
	public boolean[] getAction(Environment observation) {
		if (toggleCountdown-- < 0) toggleAgents();
		
		// astar needs to be told of every scene, even if the result isn't used
		if (!(activeAgent instanceof CS478_AStar_Backprop_Learner))
			astar.getAction(observation);
		
		return activeAgent.getAction(observation);
	}

	@Override
	public AGENT_TYPE getType() {
		return AGENT_TYPE.AI;
	}

	@Override
	public String getName() {
		return activeAgent.getName();
	}

	@Override
	public void setName(String name) {
	}

}
